package com.cqdev.washer.biz.service.impl;

import com.cqdev.washer.biz.model.BusinessParamBO;
import com.cqdev.washer.biz.service.BusinessService;
import com.cqdev.washer.biz.service.ExecuteService;
import com.cqdev.washer.infra.util.JsonMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Yunfei.li
 */
@Service("business")
public class BusinessServiceImpl implements BusinessService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private final ExecutorService newFixedThreadPool = new ThreadPoolExecutor(
            5, 10, 10, TimeUnit.SECONDS, new LinkedBlockingDeque());
    private final ExecuteService executeService;

    public BusinessServiceImpl(ExecuteService executeService) {
        this.executeService = executeService;
    }

    @Override
    public void fetchOffer(BusinessParamBO param) {
        logger.info("trigger fetch offer,param:{}", JsonMapper.nonEmptyMapper().toJson(param));
        Long projectId = param.getProjectId();
        Runnable runnable = () -> {
            executeService.exec(projectId);
        };
        newFixedThreadPool.execute(runnable);

    }


}
